(function() {
    var Dom = YAHOO.util.Dom,
    Event = YAHOO.util.Event,
    Sel = YAHOO.util.Selector,
    q = Sel.query;
    YAHOO.log('tabview.js loaded', 'info', 'tabview.js');
    //Set the time on the home screen
    YAHOO.gosearch.app.setTime = function() {
        var d = new Date();
        var weekday = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
        var h = d.getHours(), a = 'am';
        if (h >= 12) {
            a = 'pm';
            if (h > 12) {
                h = (h - 12);
            }
        }

        var dy = d.getDate();
        if (dy < 10) {
            dy = '0' + dy;
        }

        var m = (d.getMonth() + 1);
        if (m < 10) {
            m = '0' + m;
        }

        var dt = weekday[d.getDay()] + ' ' + m + '/' + dy + '/' + d.getFullYear() + ' ' + h + ':' + d.getMinutes() + ' ' + a;
        YAHOO.util.Dom.get('datetime').innerHTML = dt;
        YAHOO.util.Dom.get('calDateStr').innerHTML = m + '/' + dy + '/' + d.getFullYear();
        YAHOO.log('Setting the time/date string to: ' + dt, 'info', 'tabview.js');
    };

    //Method to Resize the tabview
    YAHOO.gosearch.app.resizeTabView = function() {
        var ul = YAHOO.gosearch.app.tabView._tabParent.offsetHeight;
        Dom.setStyle(YAHOO.gosearch.app.tabView._contentParent, 'height', ((YAHOO.gosearch.app.layout.getSizes().center.h - ul) - 2) + 'px');
    };

    //Listen for the layout resize and call the method
    YAHOO.gosearch.app.layout.on('resize', YAHOO.gosearch.app.resizeTabView);
    //Create the tabView
    YAHOO.log('Creating the main TabView instance', 'info', 'tabview.js');
    YAHOO.gosearch.app.tabView = new YAHOO.widget.TabView();
    //Create the Home tab
    YAHOO.gosearch.app.tabView.addTab( new YAHOO.widget.Tab({
        //Inject a span for the icon
        label: '<span></span>主页',
        id: 'homeView',
        content: '<div id="welcomeWrapper"><h2>Welcome to the home screen</h2><span id="datetime"></span><div id="weather"><span><em></em><strong>Sunnyvale, CA</strong></span></div></div><div id="news" class="yui-navset"><ul class="yui-nav"><li class="selected" id="newsTop"><a href="#tab1"><em>Top Stories</em></a></li><li id="newsWorld"><a href="#tab2"><em>World</em></a></li><li id="newsEnt"><a href="#tab3"><em>Entertainment</em></a></li><li id="newsSports"><a href="#tab4"><em>Sports</em></a></li></ul><div class="yui-content"><div></div><div></div><div></div><div></div></div></div>'
        
    }));

    //Create the Go game hall  tab
    var tabGoHallView = new YAHOO.widget.Tab({
        //Inject a span for the icon
        label: '<span></span>Game Hall',
        id: 'Game Hall',
        content: '<div id="game-hall-container"><div>',
        active: true
    });
    YAHOO.gosearch.app.tabView.addTab(tabGoHallView);
    
    //Create the Go play  tab
    var tab_goSearchView = new YAHOO.widget.Tab({
        //Inject a span for the icon
        label: '<span></span>对弈',
        id: 'goSearchView',
        content: '<div id="go-formular-container"><div>'
    });
    //YAHOO.gosearch.app.tabView.setStyle('overflow','auto');
    Dom.setStyle(YAHOO.gosearch.app.tabView._contentParent, 'overflow', 'auto');
    YAHOO.gosearch.app.tabView.addTab(tab_goSearchView);
    YAHOO.gosearch.app.tabView.on('activeTabChange', function(ev) {
        //Is an editor present?
        if (YAHOO.gosearch.app.editor) {
            if (ev.newValue.get('id') == 'composeView') {
                YAHOO.log('Showing the ediitor', 'info', 'tabview.js');
                YAHOO.gosearch.app.editor.show();
                YAHOO.gosearch.app.editor.set('disabled', false);
            } else {
                YAHOO.log('Hiding the editor', 'info', 'tabview.js');
                YAHOO.gosearch.app.editor.hide();
                YAHOO.gosearch.app.editor.set('disabled', true);
            }
        }
        //Resize to fit the new content
        YAHOO.gosearch.app.layout.resize();
    });
    //Add the tabview to the center unit of the main layout
    var el = YAHOO.gosearch.app.layout.getUnitByPosition('center').get('wrap');
    YAHOO.gosearch.app.tabView.appendTo(el);

    //resize the TabView
    YAHOO.gosearch.app.resizeTabView();
    //Set the time on the home screen
    YAHOO.gosearch.app.setTime();
    //Setup the interval to update the time
    setInterval(YAHOO.gosearch.app.setTime, 60000);


    YAHOO.log('Fetch the news feed', 'info', 'tabview.js');
    YAHOO.util.Get.script('js/news.js');
    
    Event.addListener(q("#logout_button"), "click", function(e) {
		YAHOO.gosearch.app.getGameHall().logout(e);
	});
    

    var el_game_hall = Dom.get('game-hall-container');
    var gh = new YAHOO.gosearch.go.gameHall.GameHall();
    YAHOO.gosearch.app.initGameHallContainer = function(){
        try{            
            gh.render(el_game_hall);
        }catch(e){
            alert(e);
        }
    }
    YAHOO.gosearch.app.initGameHallContainer();
    YAHOO.gosearch.app.addNewGameRoom = function(){
        gh.add();
    }
    YAHOO.gosearch.app.getGameHall = function(){
        return gh;
    }

    /**
         * draw formular container
         */
    YAHOO.gosearch.app.drawFormularContainer = function (){
        try{
            var el = Dom.get('go-formular-container');
            var fb = new YAHOO.gosearch.go.formularyboard.Formularyboard();
            fb.render(el);
        }catch(e){
            alert(e);
        }
            
    }

    YAHOO.gosearch.app.drawFormularContainer();

})();
